package com.jd.scaffold.web.controller.base;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import com.alibaba.fastjson.JSON;
import com.jd.common.web.LoginContext;
import com.jd.common.web.cookie.CookieUtils;
import com.jd.scaffold.common.entity.Pagination;
import com.jd.scaffold.common.entity.Result;
import com.jd.vender.common.url.VenderUrl;

public class BaseController{

	private CookieUtils cookieUtils = new CookieUtils();

	/**
	 * 获取京东pin
	 * 
	 * @param request
	 * @return
	 */
	public String getErpPin() {
		LoginContext ctx = LoginContext.getLoginContext();
		if (null != ctx) {
			String pin = ctx.getPin();
			return pin;
		} else {
			return "";
		}
	}

	/**
	 * 获取京东nick
	 * 
	 * @param request
	 * @return
	 */
	public String getNick() {
		LoginContext ctx = LoginContext.getLoginContext();
		if (null != ctx) {
			String nick = ctx.getNick();
			return nick;
		} else {
			return "";
		}
	}

	/**
	 * 描述:查询分页结果转换成页面信息返回
	 * <p>创建人：jrzhangwei , 2014年12月18日 上午9:35:18</p>
	 * 
	 * @param result
	 *            查询结果分页信息
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	protected String paginationResultToJson(Result result) {
		Map<String, Object> pageMap = new HashMap<String, Object>();
		pageMap.put("hasError", false);
		pageMap.put("error", "");
		pageMap.put("rows", "");
		pageMap.put("results", "");
		if (result == null) {
			pageMap.put("hasError", true);
			pageMap.put("error", "系统异常");
		}
		if (result.isSuccess()) {
			Pagination page = (Pagination) result.getData();
			if (page != null) {
				pageMap.put("rows", page.getDatas());
				pageMap.put("results", page.getTotalCount());
			}
		}
		return JSON.toJSONString(pageMap);
	}

	/**
	 * 描述:查询分页结果转换成页面信息返回
	 * <p>创建人：jrzhangwei , 2014年12月18日 上午9:35:18</p>
	 * 
	 * @param result
	 *            查询结果分页信息
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	protected String resultToJson(Result result) {
		Map<String, Object> pageMap = new HashMap<String, Object>();
		pageMap.put("hasError", false);
		pageMap.put("error", "");
		pageMap.put("rows", "");
		pageMap.put("results", "");
		if (result == null) {
			pageMap.put("hasError", true);
			pageMap.put("error", "系统异常");
		}
		if (result.isSuccess()) {
			if (result.getData() != null) {
				pageMap.put("rows", result.getData());
				pageMap.put("results", 1);
			}
		}
		return JSON.toJSONString(pageMap);
	}

	protected void logOut(HttpServletRequest request, HttpServletResponse response) {
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			cookieUtils.deleteCookie(response, cookie.getName());
		}
	}

	@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CustomDateEditor editor = new CustomDateEditor(df, true);
		binder.registerCustomEditor(Date.class, editor);
	}
}
